﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using MySql.Data.MySqlClient;
using Halk.Model;


namespace Halk.Dao
{
    public class DaoAttachs : DaoBase
    {
        public DaoAttachs(string connectionString)
            : base(connectionString)
        {
            ConnectionString = connectionString;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(AttachInfo model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into jieqi_article_attachs(");
            strSql.Append("articleid,chapterid,name,class,postfix,size,hits,needexp,uptime,attachid)");
            strSql.Append(" values (");
            strSql.Append("?articleid,?chapterid,?name,?class,?postfix,?size,?hits,?needexp,?uptime,?attachid)");
            MySqlParameter[] parameters = {
					new MySqlParameter("?articleid", MySqlDbType.Int32,11),
					new MySqlParameter("?chapterid", MySqlDbType.Int32,11),
					new MySqlParameter("?name", MySqlDbType.VarChar,80),
					new MySqlParameter("?class", MySqlDbType.VarChar,30),
					new MySqlParameter("?postfix", MySqlDbType.VarChar,30),
					new MySqlParameter("?size", MySqlDbType.Int32,11),
					new MySqlParameter("?hits", MySqlDbType.Int32,11),
					new MySqlParameter("?needexp", MySqlDbType.Int32,11),
					new MySqlParameter("?uptime", MySqlDbType.Int32,11),
                                          new MySqlParameter("?attachid", MySqlDbType.Int32,11)};
            parameters[0].Value = model.articleid;
            parameters[1].Value = model.chapterid;
            parameters[2].Value = model.name;
            parameters[3].Value = model.cclass;
            parameters[4].Value = model.postfix;
            parameters[5].Value = model.size;
            parameters[6].Value = model.hits;
            parameters[7].Value = model.needexp;
            parameters[8].Value = model.uptime;
            parameters[9].Value = model.attachid;
            return MySqlHelper.ExecuteNonQuery(Connection, System.Data.CommandType.Text, strSql.ToString(), parameters) >= 0 ? true : false;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Halk.Model.AttachInfo model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update jieqi_article_attachs set ");
            strSql.Append("articleid=?articleid,");
            strSql.Append("chapterid=?chapterid,");
            strSql.Append("name=?name,");
            strSql.Append("class=?class,");
            strSql.Append("postfix=?postfix,");
            strSql.Append("size=?size,");
            strSql.Append("hits=?hits,");
            strSql.Append("needexp=?needexp,");
            strSql.Append("uptime=?uptime");
            strSql.Append(" where attachid=?attachid");
            MySqlParameter[] parameters = {
					new MySqlParameter("?articleid", MySqlDbType.Int32,11),
					new MySqlParameter("?chapterid", MySqlDbType.Int32,11),
					new MySqlParameter("?name", MySqlDbType.VarChar,80),
					new MySqlParameter("?class", MySqlDbType.VarChar,30),
					new MySqlParameter("?postfix", MySqlDbType.VarChar,30),
					new MySqlParameter("?size", MySqlDbType.Int32,11),
					new MySqlParameter("?hits", MySqlDbType.Int32,11),
					new MySqlParameter("?needexp", MySqlDbType.Int32,11),
					new MySqlParameter("?uptime", MySqlDbType.Int32,11),
					new MySqlParameter("?attachid", MySqlDbType.Int32,11)};
            parameters[0].Value = model.articleid;
            parameters[1].Value = model.chapterid;
            parameters[2].Value = model.name;
            parameters[3].Value = model.cclass;
            parameters[4].Value = model.postfix;
            parameters[5].Value = model.size;
            parameters[6].Value = model.hits;
            parameters[7].Value = model.needexp;
            parameters[8].Value = model.uptime;
            parameters[9].Value = model.attachid;

            return MySqlHelper.ExecuteNonQuery(Connection, System.Data.CommandType.Text, strSql.ToString(), parameters) >= 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int attachid)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from jieqi_article_attachs ");
            strSql.Append(" where attachid=?attachid");
            MySqlParameter[] parameters = {
					new MySqlParameter("?attachid", MySqlDbType.Int32)
			};
            parameters[0].Value = attachid;
            return MySqlHelper.ExecuteNonQuery(Connection, System.Data.CommandType.Text, strSql.ToString(), parameters) >= 0;
        }

        /// <summary>
        /// 获取最大ATTACH ID
        /// </summary>
        /// <returns></returns>
        public Int32 GetAttachMaxID()
        {
            var strSql = new StringBuilder();
            strSql.Append("select max(attachid) as  attachid from jieqi_article_attachs");
            using (MySqlDataReader dr = MySqlHelper.ExecuteReader(Connection, System.Data.CommandType.Text, strSql.ToString(), null))
            {
                while (dr.Read())
                {
                    if (!string.IsNullOrEmpty(dr[0].ToString()))
                    {
                        return Int32.Parse(dr[0].ToString());
                    }
                }

            }
            return 0;
        }

        /// <summary>
        /// 将小说附件更新为无效
        /// </summary>
        /// <param name="articleid"></param>
        /// <returns></returns>
        public bool UpdateAttachInvalid(int articleid)
        {
            var strSql = new StringBuilder();
            strSql.Append("update jieqi_article_attachs  set isactive ='0' ");
            strSql.Append(" where articleid=?articleid");
            MySqlParameter[] parameters = {
					new MySqlParameter("?articleid", MySqlDbType.Int32)
			};
            parameters[0].Value = articleid;
            return MySqlHelper.ExecuteNonQuery(Connection, System.Data.CommandType.Text, strSql.ToString(), parameters) >= 0;
        }

        public List<AttachInfo> GetAttachs(ChapterInfo chapter)
        {
            var list=new List<AttachInfo>();
            string strSql = string.Format("select * from jieqi_article_attach where articleid = '{0}' and chapterid='{1}' order by attachid", chapter.articleid.ToString(CultureInfo.InvariantCulture), chapter.chapterid.ToString(CultureInfo.InvariantCulture));
            using (MySqlDataReader dr = MySqlHelper.ExecuteReader(Connection, System.Data.CommandType.Text, strSql.ToString(CultureInfo.InvariantCulture), null))
            {
                while (dr.Read())
                {
                    var model = new AttachInfo();
                    if (dr["attachid"] != null && dr["attachid"].ToString() != "")
                    {
                        model.attachid = int.Parse(dr["attachid"].ToString());
                    }
                    if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                    {
                        model.articleid = int.Parse(dr["articleid"].ToString());
                    }
                    if (dr["chapterid"] != null && dr["chapterid"].ToString() != "")
                    {
                        model.chapterid = int.Parse(dr["chapterid"].ToString());
                    }
                    if (dr["name"] != null)
                    {
                        model.name = dr["name"].ToString();
                    }
                    if (dr["class"] != null)
                    {
                        model.cclass = dr["class"].ToString();
                    }
                    if (dr["postfix"] != null)
                    {
                        model.postfix = dr["postfix"].ToString();
                    }
                    if (dr["size"] != null && dr["size"].ToString() != "")
                    {
                        model.size = int.Parse(dr["size"].ToString());
                    }
                    if (dr["hits"] != null && dr["hits"].ToString() != "")
                    {
                        model.hits = int.Parse(dr["hits"].ToString());
                    }
                    if (dr["needexp"] != null && dr["needexp"].ToString() != "")
                    {
                        model.needexp = int.Parse(dr["needexp"].ToString());
                    }
                    if (dr["uptime"] != null && dr["uptime"].ToString() != "")
                    {
                        model.uptime = int.Parse(dr["uptime"].ToString());
                    }
                    list.Add(model);
                }
            }
            return list;
        }
    }
}
